package com.yawn.zerodefect.util.poi;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.RichTextString;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     &lt;dependency>
 *         &lt;groupId>org.apache.poi&lt;/groupId>
 *         &lt;artifactId>poi&lt;/artifactId>
 *         &lt;version>3.10-FINAL&lt;/version>
 *     &lt;/dependency>
 * </pre>
 *
 * @author yawn
 * @date 2018-03-15 9:14
 */
public class ExcelUtil {

    private ExcelUtil() {
    }

    public static <T> byte[] exportList(List<T> list, List<String> header) {

        //
        HSSFWorkbook workbook = new HSSFWorkbook();
        //
        HSSFSheet sheet = workbook.createSheet();
        //
        HSSFRow headerRow = sheet.createRow(0);
        for (int i = 0; i < header.size(); i++) {
            HSSFCell cell = headerRow.createCell(i);
            RichTextString content = new HSSFRichTextString(header.get(i));
            cell.setCellValue(content);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        }
        for (T entity : list) {
            int rowNum = sheet.getLastRowNum() + 1;
            HSSFRow row = sheet.createRow(rowNum);
            Class<?> clazz = entity.getClass();
            Field[] fields = clazz.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                HSSFCell cell = row.createCell(i);
                try {
                    fields[i].setAccessible(true);
                    Object value = fields[i].get(entity);
                    setCellByValue(cell, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return workbook.getBytes();
    }

    private static void setCellByValue(HSSFCell cell, Object value) {
        if (value == null) {
            cell.setCellType(HSSFCell.CELL_TYPE_BLANK);
            return;
        }
        RichTextString content;
        Class<?> clazz = value.getClass();
        if (clazz == Integer.class || clazz == Long.class
                || clazz == Short.class || clazz == Double.class || clazz == Float.class) {
            cell.setCellValue(Double.parseDouble(String.valueOf(value)));
            //指定单元格格式：数值、公式或字符串
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        } else if (clazz == String.class) {
            content = new HSSFRichTextString((String) value);
            cell.setCellValue(content);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        } else {
            content = new HSSFRichTextString(value.toString());
            cell.setCellValue(content);
            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        }
    }

    public static void main(String[] args) throws IOException {
        List<Car> carList = new ArrayList<Car>();
        for (int i = 0; i < 1000; i++) {
            Car car = new Car(i, "car" + i, 10 + i);
            carList.add(car);
        }
        List<String> header = new ArrayList<String>();
        header.add("id");
        header.add("name");
        header.add("年龄");
        File file = new File("C:/Users/yawn/Desktop/test.xls");
//        exportList(carList, header, new FileOutputStream(file));
    }
}
